import csv

def database(base="Domestic"):
    csvfiles={"Foreign":'data/Foreign.csv', 
              "Domestic":'data/Domestic.csv',
              "Dataset":'data/Dataset.csv',
              "Isomerism":'data/Isomerism.csv',
              "Startups":'data/Startups.csv',
              }
    rows=[]
    # 打开CSV文件
    with open(csvfiles[base], mode='r', encoding='utf-8') as file:
        # 创建CSV阅读器
        reader = csv.reader(file)
        
        # 逐行读取
        for row in reader:
            #print(row)  # row是一个列表，包含了CSV文件中的每个字段
            rows.append(row)
    if not len(rows):return {}
    keys=rows[0]
    values=rows[1:]
    data={}
    for v in values:
        kv = {}
        key=''
        for kk,vv in zip(keys,v):
            if kk in [keys[0], keys[1]]:
                key += vv
            vv=vv.split(';')
            if len(vv)<2:
                x=2-len(vv)
                vv.extend(['']*x)
            vvv=[]
            # will ignore "" in generated url if last character is /
            for v in vv:
                if len(v) and v[-1]=='/':
                    v=v[:-1]
                vvv.append(v)
            kv[kk]=vvv
        data[key]=kv
    return data



if __name__ == '__main__': 
    for key in ["Foreign", "Domestic", "Isomerism","Dataset","Startups"]:
        data = database(key)
        print()
        
